import legendJson from '@/config/legendJson.js'
import moment from 'moment'
export const getColorIndex = (legend, value) => {
	let bgColor = ''
	let index = ''
	// 图例为倒序
	if (legend.order === 'desc') {
		for (var i = 0; i < legend.items.length; i++) {
			if (value >= legend.items[i].min) {
				bgColor = legend.items[i].color
				index = i
				break
			}
		}
	} else {
		for (var j = legend.items.length - 1; j >= 0; j--) {
			if (value >= legend.items[j].min) {
				bgColor = legend.items[j].color
				index = j
				break
			}
		}
	}
	return { bgColor, index }
}

export const getlegendOptions = (type) => {
	const legendOptions = {
		legendData: legendJson.find((el) => el.type == type),
		layout: 'h',
		labelAlign: 'middle',
		labelPosition: 'left',
		labelClass: 'legend-label',
		titleClass: 'legend-title',
		unitClass: 'legend-unit',
		itemHeight: '28',
		textGapShow: type == 'Wind' ? 2 : 1
	}
	return legendOptions
}

export const getlegend = (type) => {
	return legendJson.find((el) => el.type == type)
}

// l  n  r  过去现在以后 lh过去几小时 nh现在几小时
export const getTime = (hours = 0, type = 'n') => {
	if (type == 'l') {
		return moment().subtract(hours, 'hours').format('YYYY-MM-DD HH:00')
	}
	if (type == 'r') {
		return moment().add(hours, 'hours').format('YYYY-MM-DD HH:00')
	}
	if (type == 'n') {
		return moment().format('YYYY-MM-DD HH:00')
	}
	if (type == 'lh') {
		return moment().subtract(1, 'days').hours(hours).format('YYYY-MM-DD HH:00')
	}
	if (type == 'nh') {
		return moment().hours(hours).format('YYYY-MM-DD HH:00')
	}
}
